#define _CRT_SECURE_NO_WARNINGS
// Problem 1
class Solution {
public:
    int mySqrt(int x) {
        long l = 0, r = x;
        while (l < r) {
            long mid = (l + r + 1) / 2;
            if (mid * mid <= x) {
                l = mid;
            }
            else {
                r = mid - 1;
            }
        }
        return r;
    }
};

//Problem 2
class Solution {
public:
    char nextGreatestLetter(vector<char>& letters, char target) {
        int l = 0, r = letters.size() - 1;
        while (l < r)
        {
            int mid = (l + r) / 2;
            if (letters[mid] > target) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        return letters[r] > target ? letters[r] : letters[0];
    }
};